package com.bike.controller.system;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bike.controller.BaseController;
import com.bike.entity.SysUser;
import com.bike.service.impl.SystemServiceImpl;
import com.bike.util.PageData;

@Controller
@RequestMapping("sysUser")
public class SysUserController extends BaseController{
	 
	@Autowired
	private SystemServiceImpl systemService;
 
	/**
	 * 进入菜单列表
	 * @return
	 */
	@RequestMapping("toList")
	public String toList(){
		return "/system/user/user";
	}
	/**
	 * 进入菜单列表
	 * @return
	 */
	@RequestMapping("toAgentUserEdit")
	public String toAgentUserEdit(HttpServletRequest request,Model model){
		PageData pd=PageData.getPageDataByPage(request);
		model.addAttribute("pd", pd);
		if(pd.getLong("userId")>0){
			model.addAttribute("pd", systemService.selectBySysUserId(pd.getLong("userId")));
		} 
		return "/system/user/toAgentUserEdit";
	}
	/**
	 * 进入菜单列表
	 * @return
	 */
	@RequestMapping("toBusinessUserList")
	public String toSchoolUserList(HttpServletRequest request,Model model){
		PageData pd=PageData.getPageDataByPage(request);
		model.addAttribute("pd", pd);
		return "/system/user/toBusinessUserList";
	}
	/**
	 * 列表
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public PageData list(HttpServletRequest request,Model model){
		PageData pd=PageData.getPageDataByPage(request);
		return systemService.selectSysUserList(pd);
	}
	/**
	 * 去编辑
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("toEdit")
	public String toEdit(HttpServletRequest request,Model model){
		PageData pd=PageData.getPageDataByPage(request);
		if(pd.getLong("userId")>0){
			model.addAttribute("pd", systemService.selectBySysUserId(pd.getLong("userId")));
		} 
		model.addAttribute("role", systemService.selectTreeDtoRole());
		model.addAttribute("record", pd);
		
		return "/system/user/userEdit";
	}
	/**
	 * 去编辑
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("toBusinessUserEdit")
	public String toSchoolAdminEdit(HttpServletRequest request,Model model){
		PageData pd=PageData.getPageDataByPage(request);
		if(pd.getLong("userId")>0){
			model.addAttribute("pd", systemService.selectBySysUserId(pd.getLong("userId")));
		} 
		model.addAttribute("role", systemService.selectTreeDtoRole());
		model.addAttribute("record", pd);
		
		return "/system/user/businessUserEdit";
	}
	/**
	 * 搜素
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("toSearch")
	public String toSearch(HttpServletRequest request,Model model){
		PageData pd=PageData.getPageDataByPage(request);
		model.addAttribute("pd", pd);
		model.addAttribute("role", systemService.selectTreeDtoRole());
		return "/system/user/userSearch";
	}
	/**
	 * 保存
	 * @param request
	 * @param sysUser
	 * @return
	 */
	@RequestMapping("save")
	@ResponseBody
	public Map<String,Object> save(HttpServletRequest request,SysUser sysUser){
		try {
			PageData pd=PageData.getPageDataByPage(request);
			systemService.saveOrUpdate(pd,sysUser);
			return getSuccessMap();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return getErrorMap("失败");
	}
	/**
	 * 删除
	 * @param request
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Map<String,Object> delete(HttpServletRequest request){
		PageData pd=PageData.getPageDataByPage(request);
		if(pd.getLong("userId")>0){
			SysUser user=new SysUser();
			user.setUserId(pd.getLong("userId"));
			user.setStatus(1);
			systemService.updateUser(user);
			return getSuccessMap();
		}
		return getErrorMap("失败");
	}
	//明细
	@RequestMapping("detail")
	@ResponseBody
	public SysUser detail(HttpServletRequest request){
		PageData pd=PageData.getPageDataByPage(request);
		if(pd.getLong("userId")>0){
			SysUser user= systemService.selectBySysUserId(pd.getLong("userId"));
			user.setPwd("");
			return user;
		}
		return null;
	}
	
	/**
	 * 查用户列表
	 * @param request
	 * @return
	 */
	@RequestMapping("userList")
	@ResponseBody
	public List<SysUser> userList(HttpServletRequest request){
		PageData pd=PageData.getPageDataByPage(request);
		 
		List<SysUser>  userList= systemService.selectBySysUser(pd);
			 
		return userList;
	}
 

}
